package test1;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson2.JSONObject;
import com.sun.org.apache.xerces.internal.dom.ElementImpl;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.DomSerializer;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.springframework.util.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.util.HashMap;
import java.util.Map;





public class 渠加省投资项目在审批监管台 {

    public static String get_by_xpath(String xpath,String sqlprint,int page){
        StringBuffer result=new StringBuffer();

        try {
            Map p=new HashMap<>();
            p.put("project_name","数据");
            p.put("item_name","权限内企业、事业单位、社会团体等投资建设的固定资产投资项目备案");
            p.put("dept_name","");
            //p.put("jumpPage","1");
            p.put("jumpPage",String.valueOf(page));
            p.put("page",String.valueOf(page));
            //p.put("leafNumber","1");
            p.put("leafNumber",String.valueOf(page));
            p.put("order","");
            p.put("action","index");
            p.put("sqlprint",sqlprint);
            p.put("taxis","");
            p.put("pagecount","50");
            p.put("totalPage","10000");


            /*
            project_name: 数据
            item_name: 权限内企业、事业单位、社会团体等投资建设的固定资产投资项目备案
            dept_name:
            deal_time:
            project_code:
            project_type:
            current_state:
            jumpPage: 1
            page: 1
            leafNumber:
            order:
            sqlprint: PjzfhVxXmuVQuZ1Xoh8Q+bTggG7L6YRq8D3Gn92c0FkD1bVffsQIQXMKU66zp+m7SNrZ3xdXjMU1
            6pMWuR95lMDgkSVrPM9bt/JtAkDKoPehxJncZNhtDZxWQNwlawryhvGRY3SlVudKa3NdBRzWoBX6
            PfJjHltHxIgOUs/T5LHRRHz3R7mO8IMeNEWTVl7i+qSS2u7iLx4dWWVECzDg5NQg7bDjwzbcVPMX
            VZlx9R4Uiq+0zuFLr4XI6lT2Ex1yKNHWqvJzBtTmmf9lBYLP6UQeOLfEGT408+6g7YM5oN3J4fpn
            2ZpWi9jZL85iRTC7R05Ntya4+CyUfwESanXZTbfjA9aR6IiH
            taxis:
            pagecount: 10
            totalPage: 123
            */

            System.out.println(new JSONObject(p).toJSONString());



            HttpReqModel reqModel = new HttpReqModel();
            reqModel.setUrl("https://hlj.tzxm.gov.cn/detail/bljgmore");
            reqModel.setParams(p);
            reqModel.setHeader(p);
            reqModel.setReadTimeout(5000);

            HttpResqModel rtn=HttpUtil.httpPost(reqModel);
            //System.out.println(rtn.getReturnMsg());
            String html=rtn.getReturnMsg();

            File f1=new File("1.html");
            FileUtil.writeString(html,f1,"utf-8");

            // 解析 HTML 页面内容
            HtmlCleaner hc = new HtmlCleaner();
            TagNode tn = hc.clean(html);
            Document document = new DomSerializer(new CleanerProperties()).createDOM(tn);

            // 匹配获取需要的数据
            XPath xPath = XPathFactory.newInstance().newXPath();
            //String exp = "//*[@id=\"MainTable\"]/tbody/tr[position()>=5 and position()<=22]/td[position()=1 or (position()>=9 and position()<=13)]";
            //String exp = "//*[@id=\"page\"]/li[2]/a/span";
            String exp=xpath;
            NodeList nodes = (NodeList) xPath.evaluate(exp, document, XPathConstants.NODESET);

            int huan_hang=5;

            for (int length = nodes.getLength(), i = 0; i < length; i++) {
                Node item = nodes.item(i);
                //result.append(item.getTextContent().toString().trim());
                result.append(item.getTextContent().toString().replace("\r","").replace("\n","").trim());
                //result.append(item.getTextContent().toString().replace("\r","").trim());
                //System.out.println();
                if(nodes.getLength()>1){
                    result.append("\t");
                    if(i!=0&&i==huan_hang){
                        result.append("\n");
                        huan_hang+=6;
                    }
                }


            }



        }catch(Exception e){
            e.printStackTrace();
        }

        return result.toString();
    }



    public static String get_attr_by_xpath(String xpath,String attr_name){
        String result="";

        try {
            Map p=new HashMap<>();
//            p.put("project_name","数据");
//            p.put("item_name","权限内企业、事业单位、社会团体等投资建设的固定资产投资项目备案");
//            p.put("dept_name","");
//            p.put("jumpPage",1);
//            p.put("page","");
//            p.put("leafNumber",1);
//            p.put("order","");
//            p.put("action","index");
//            p.put("sqlprint","");
//            p.put("taxis","");
//            p.put("pagecount",10);
//            p.put("totalPage",10000);

            HttpReqModel reqModel = new HttpReqModel();
            reqModel.setUrl("https://hlj.tzxm.gov.cn/detail/bljgmore");
            reqModel.setParams(p);

            HttpResqModel rtn=HttpUtil.httpPost(reqModel);
            //System.out.println(rtn.getReturnMsg());
            String html=rtn.getReturnMsg();

            // 解析 HTML 页面内容
            HtmlCleaner hc = new HtmlCleaner();
            TagNode tn = hc.clean(html);
            Document document = new DomSerializer(new CleanerProperties()).createDOM(tn);

            // 匹配获取需要的数据
            XPath xPath = XPathFactory.newInstance().newXPath();
            //String exp = "//*[@id=\"MainTable\"]/tbody/tr[position()>=5 and position()<=22]/td[position()=1 or (position()>=9 and position()<=13)]";
            //String exp = "//*[@id=\"page\"]/li[2]/a/span";
            String exp=xpath;
            NodeList nodes = (NodeList) xPath.evaluate(exp, document, XPathConstants.NODESET);

            for (int length = nodes.getLength(), i = 0; i < length; i++) {
                Node item = nodes.item(i);
                //result+=item.getTextContent();
                //result+=((ElementImpl) item).getAttribute("value");
                result+=((ElementImpl) item).getAttribute(attr_name);
                //System.out.println();
            }



        }catch(Exception e){
            e.printStackTrace();
        }

        return result;
    }


    public static void main(String[] args) {

        //String s2=get_attr_by_xpath("//*[@id=\"webForm\"]/sitename/sitedomain/siteidcode/div[8]/div[4]/div/input[5]","value");
        String s1=get_by_xpath("//*[@id=\"page\"]/li[2]/a/span","",1);
        int pagecount=Integer.parseInt(s1.split("/")[1]);
        //System.out.println(s1);
        System.out.println(pagecount);
        File file=new File("1.txt");
        FileUtil.writeString("",file,"utf-8");
        //FileUtil.cleanEmpty(file);

        s1=get_by_xpath("//*[@id=\"webForm\"]/sitename/sitedomain/siteidcode/div[8]/div[4]/table/tbody/tr/td","",11);
        System.out.println(s1);


        /*for (int i=1;i<=pagecount;i++){
            //s2=get_attr_by_xpath("//*[@id=\"webForm\"]/sitename/sitedomain/siteidcode/div[8]/div[4]/div/input[5]","value");
            s1=get_by_xpath("//*[@id=\"webForm\"]/sitename/sitedomain/siteidcode/div[8]/div[4]/table/tbody/tr/td","",i);
            if(StringUtils.isEmpty(s1)){
                i=i-1;
            }
            System.out.println(s1);
            FileUtil.appendString(s1,file,"utf-8");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }*/
    }
}

